const app = getApp(),
  pageSize = 10;

const stateOptions = [{
  value: "",
  name: "全部"
}, {
  value: 1,
  name: "正常"
}, {
  value: 2,
  name: "冻结"
}]
Page({
  data: {
    empty: false,
    superior_id: "", //0合伙人 其他加盟商
    from: "", //页面来源  1一级页面进入  2加盟管理的二级页面进入
    list: [],
    conditions: {
      account: "", //搜索 账号
      phone: "", //搜索 电话
      name: "", //搜索 姓名
      stateIndex: "", //搜索 状态
    },
    isFilter: false,
    drawer: false,
    loadmore: false,
    nomore: false,
    stateOptions,
    total: 0,
  },

  onLoad: function (options) {
    let {
      from = 1
    } = {
      options
    };
    this.setData({
      from: from == 1 ? 1 : 2
    })
    this.page = 1;
    this.getPartnerInfo();
  },


  onPullDownRefresh: function () {
    this.reloadData();
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      this.page += 1;
      this.getEngineeringList();
    }
  },

  reloadData() {
    this.page = 1;
    this.setData({
      list: [],
      loadmore: false,
      nomore: false,
      total: 0
    })
    wx.showLoading();
    this.getEngineeringList();
  },



  formatIdCard(idCard) {
    return idCard.replace(/^(.{6})(?:\d+)(.{4})$/, "$1****$2");
  },

  deleteEngineer(e) {
    const {
      id
    } = e.currentTarget.dataset;
    wx.showModal({
      content: "确定要删除该维修员?",
      success: res => {
        let {
          cancel,
          confirm
        } = res;
        if (confirm == true) {
          wx.showLoading({
            mask: true
          })
          app.ajax({
            url: "Partner/EquipmentManagement/del",
            data: {
              id,
              type: this.data.type
            },
            success: res => {
              wx.hideLoading();
              if (res.data.code == 1000) {
                app.showToast(res.data.message, "success");
                setTimeout(() => {
                  this.reloadData()
                }, 1500)
              } else {
                app.showToast(res.data.message);
              }
            },
            error: e => {
              wx.hideLoading();
              app.showToast("网络错误!请稍后再试~~~")
            }
          })
        }
      }
    })
  },

  lookEngineer(e) {
    const {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '../detail/detail?id=' + id,
    })
  },

  //新增维修员
  add() {
    wx.navigateTo({
      url: '../add/add?type=' + this.data.type
    })
  },
  editEngineer(e) {
    const {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '../add/add?type=' + this.data.type + "&id=" + id,
    })
  },




  //获取合伙人信息，拿到角色是合伙人还是加盟商
  getPartnerInfo() {
    app.ajax({
      url: "Partner/User/getPartnerInfo",
      success: res => {
        if (res.data.code == 200) {
          let {
            superior_id
          } = res.data.data;
          let from = this.data.from,
            type;
          if (superior_id == 0) {
            type = 1;
          } else {
            if (from == 1) {
              type = 3
            } else {
              type = 2
            }
          }
          this.setData({
            superior_id: Number.parseInt(superior_id),
            type
          })
          this.getEngineeringList();
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        app.showToast("网络错误!请稍后再试~~~");
      }
    })
  },


  //获取维修员列表
  getEngineeringList() {
    app.ajax({
      url: "Partner/EquipmentManagement/index",
      data: {
        page: this.page,
        pageSize,
        type: this.data.type,
        phone: this.data.conditions.phone,
        name: this.data.conditions.name,
        account: this.data.conditions.account,
        state: this.data.conditions.stateIndex !== "" ? this.data.stateOptions[this.data.conditions.stateIndex].value : "",
      },
      success: res => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.data.code == 1000) {
          let source = res.data.data.data;
          source.forEach(current => {
            current.idcard = this.formatIdCard(current.idcard)
          });
          let list = this.data.list.concat(source);
          let total = res.data.data.page.total_number;
          this.setData({
            list,
            empty: total == 0 ? true : false,
            nomore: list.length >= total ? true : false,
            loadmore: list.length < total ? true : false,
            drawer: false,
            total,
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误!请稍后再试~~~");
      }
    })
  },

  addEngineering() {
    wx.navigateTo({
      url: '../add/add?type=' + this.data.type
    })
  },


  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },



  handleChange(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    this.setData({
      [`conditions.${name}`]: index
    })
  },



  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },

})